﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace colorgetter
{
    public partial class FormColor : Form
    {
        #region Win32 API
        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr ptr);
        [DllImport("gdi32.dll")]
        static extern int GetDeviceCaps(
        IntPtr hdc, // handle to DC
        int nIndex // index of capability
        );
        [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
        static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
        #endregion
        #region DeviceCaps常量
        const int HORZRES = 8;
        const int VERTRES = 10;
        const int LOGPIXELSX = 88;
        const int LOGPIXELSY = 90;
        const int DESKTOPVERTRES = 117;
        const int DESKTOPHORZRES = 118;
        #endregion



        public FormColor()
        {
            InitializeComponent();
        }
        MouseHook mh;
        private void Form1_Load(object sender, EventArgs e)
        {
           
         

            mh = new MouseHook();
            mh.SetHook();
            mh.MouseDownEvent += Mh_MouseDownEvent;
            mh.MouseUpEvent += Mh_MouseUpEvent;

            mh.MouseMoveEvent += Mh_MouseMoveEvent;

            this.FormClosed += FormColor_FormClosed;

            /*

            Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
            Graphics g = Graphics.FromImage(img);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
            //ScreenBody body = new ScreenBody();
            this.BackgroundImage = img;
            */
            //body.Show();


        }

        private void FormColor_FormClosed(object sender, FormClosedEventArgs e)
        {
            mh.Dispose();
        }

        private void Mh_MouseMoveEvent(object sender, MouseEventArgs e)
        {
            if (IsDisposed)
                return;
            if (!running)
                return;

            if (g_img == null)
            {
                return;
            }
            int x = e.X;
            int y = e.Y;
            if (x >= g_img.Width)
                x = g_img.Width-1;
            if (y >= g_img.Height)
                y = g_img.Height - 1;
            if (x < 0)
            {
                x = 0;
            }
            if (y < 0)
                y = 0;


            int startx = x - pictureBox1.Width / scale / 2;
            int starty = y - pictureBox1.Height / scale / 2;
            if (startx <=0)
                startx = 0;
            if (starty < 0)
                starty = 0;

            //创建新图位图
            Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            //创建作图区域
            Graphics graphic = Graphics.FromImage(bitmap);

            
            graphic.DrawImage(g_img, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height), 
                new Rectangle(startx, starty, pictureBox1.Width/scale, pictureBox1.Height / scale), GraphicsUnit.Pixel);

            graphic.DrawLine(Pens.Black, pictureBox1.Width / 2, 0, pictureBox1.Width / 2, pictureBox1.Height-1);
            graphic.DrawLine(Pens.Black,0, pictureBox1.Height / 2,  pictureBox1.Width-1, pictureBox1.Height / 2);

            graphic.Dispose();
            pictureBox1.Image = bitmap;


            Color c = g_img.GetPixel(x, y);
            panel1.BackColor = c;

            label4.Text = c.ToArgb().ToString("x").Substring(2);

            label1.Text = x.ToString();
            label2.Text = y.ToString();

            /*


            textBox1.Text = c.ToArgb().ToString("x").Substring(2);
            if (radioButton2.Checked) {
                textBox2.Text = c.R.ToString();
                textBox3.Text = c.G.ToString();
                textBox4.Text = c.B.ToString();
            }
            else
            {
                textBox2.Text = c.R.ToString("x2");
                textBox3.Text = c.G.ToString("x2");
                textBox4.Text = c.B.ToString("x2");
            }
            label1.Text = x.ToString();
            label2.Text = y.ToString();
            panel2.BackColor = c;

            */

            //g_img.Save("D:\\test\\ppp2.png");
            //bitmap.Save("D:\\test\\ppp.png");
            //this.Refresh();

        }

        private void Mh_MouseUpEvent(object sender, MouseEventArgs e)
        {
            if (!running)
                return;
        }

        private void Mh_MouseDownEvent(object sender, MouseEventArgs e)
        {



            if (!running)
                return;

            if (g_img == null)
            {
                return;
            }
            int x = e.X;
            int y = e.Y;
            if (x >= g_img.Width)
                x = g_img.Width - 1;
            if (y >= g_img.Height)
                y = g_img.Height - 1;
            if (x < 0)
            {
                x = 0;
            }
            if (y < 0)
                y = 0;

            Color c = g_img.GetPixel(x, y);                

            
            textBox1.Text = c.ToArgb().ToString("x").Substring(2);
            if (radioButton2.Checked)
            {
                textBox2.Text = c.R.ToString();
                textBox3.Text = c.G.ToString();
                textBox4.Text = c.B.ToString();
            }
            else
            {
                textBox2.Text = c.R.ToString("x2");
                textBox3.Text = c.G.ToString("x2");
                textBox4.Text = c.B.ToString("x2");
            }
            label1.Text = x.ToString();
            label2.Text = y.ToString();
            panel2.BackColor = c;


            button1.Text = "开始抓点";
            running = false;
            timer1.Stop();

        }

        bool running = false;

        private void button1_Click(object sender, EventArgs e)
        {
            if (!running)
            {
                button1.Text = "抓点中....";
                running = true;

                timer1.Start();
            }
            


        }
        Bitmap g_img = null;
        private void timer1_Tick(object sender, EventArgs e)
        {
            IntPtr hdc = GetDC(IntPtr.Zero);
            Size size = new Size();
            size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
            size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
            ReleaseDC(IntPtr.Zero, hdc);

            //int cc = System.Windows.SystemParameters.PrimaryScreenWidth;
            Bitmap img = new Bitmap(size.Width, size.Height);
            Graphics g = Graphics.FromImage(img);

           


            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), size);
            g.Dispose();
            g_img = img;
            
        }

        int scale = 2;
        private void button2_Click(object sender, EventArgs e)
        {
            scale *= 2;
            if (scale >= 16)
                scale = 16;

            label3.Text = "x" + scale;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            scale /= 2;
            if (scale <= 1)
                scale = 1;

            label3.Text = "x" + scale;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            tryParse(textBox2);
            tryParse(textBox3);
            tryParse(textBox4);
        }
        void tryParse(TextBox tx)
        {
            if (radioButton1.Checked)
            {
                int r;
                if (int.TryParse(tx.Text, out r))
                {
                    tx.Text = r.ToString("x");
                }

            }
            else
            {
                int r;
                if (int.TryParse(tx.Text, NumberStyles.HexNumber,null,out r))
                {
                    tx.Text = r.ToString();
                }
            }
        }
    }
}
